/*
 *    Copyright (c) 2023 李伟国
 *    moyu is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */

package cn.moyu.handler;

import cn.moyu.entity.base.BaseEntityException;
import cn.moyu.response.ErrorResponseData;
import cn.moyu.response.ResponseData;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseData handle416(){
        return new ErrorResponseData(416, "请求参数缺失");
    }

    /**
     * 实体类异常
     * @param e 异常
     * @return 异常返回
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BaseEntityException.class)
    public ResponseData handle400(BaseEntityException e){
        return new ErrorResponseData(HttpStatus.BAD_REQUEST.value(), e.getMessage(), e.getMessage());
    }

    /**
     * kettle数据源连接异常
     * @param e 异常
     * @return 异常返回
     */
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    @ExceptionHandler(KettleDatabaseException.class)
    public ResponseData handle4000(KettleDatabaseException e){
        return new ErrorResponseData(HttpStatus.BAD_REQUEST.value(), e.getMessage(), e.getMessage());
    }

}
